package com.daiziru.wuyanmall.service.impl;

import com.daiziru.wuyanmall.dao.ShoppingCartMapper;
import com.daiziru.wuyanmall.entity.ShoppingCart;
import com.daiziru.wuyanmall.entity.ShoppingCartVO;
import com.daiziru.wuyanmall.service.ShoppingCartService;
import com.daiziru.wuyanmall.vo.ResultStatus;
import com.daiziru.wuyanmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Override
    public ResultVO addShoppingCart(ShoppingCart cart) {
        cart.setCartTime(sdf.format(new Date()));
        int i = shoppingCartMapper.insert(cart);
        if(i > 0) {
            return new ResultVO(ResultStatus.OK,"success",null);
        } else {
            return new ResultVO(ResultStatus.NO,"fail", null);
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)//设置事务隔离级别
    public ResultVO listShoppingCartByUserId(int userId) {
        List<ShoppingCartVO> list = shoppingCartMapper.selectShopCartByUserId(userId);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", list);
        return resultVO;
    }

    @Override
    public ResultVO updateCartNum(int cartId, int cartNum) {
        int i = shoppingCartMapper.updateCartNumByCartId(cartId, cartNum);
        if(i > 0) {
            return new ResultVO(ResultStatus.OK, "success", null);
        } else {
            return new ResultVO(ResultStatus.NO, "update fail", null);
        }

    }

    /**
     * 根据id数组查询相关的购物车商品，复选框
     * @param cids
     * @return
     */
    @Override
    public ResultVO listShoppingCartsByCids(String cids) {
        String[] split = cids.split(",");
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < split.length; i++) {
            list.add(Integer.parseInt(split[i]));
        }
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShopcartByCids(list);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", shoppingCartVOS);
        return resultVO;
    }

    @Override
    public ResultVO deleteShopByCid(int cartId) {
        Example example = new Example(ShoppingCart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cartId", cartId);
        int i = shoppingCartMapper.deleteByExample(example);
        if(i > 0) {
            return new ResultVO(ResultStatus.OK, "success", null);
        } else {
            return new ResultVO(ResultStatus.NO,"delete fail", null);
        }
    }
}
